package com.tfswx.jcyw.ruleenginedesigner.api;

import com.alibaba.fastjson.JSON;
import com.tfswx.jcyw.ruleenginedesigner.bean.ApiResult;
import com.tfswx.ruleengine.compute.RuleEngine;
import com.tfswx.ruleengine.compute.bean.FormDataObject;
import com.tfswx.ruleengine.compute.bean.Gzjyjg;
import com.tfswx.ruleengine.compute.bean.RuleInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ricky
 * @date 2021/7/21
 */
@RestController
@RequestMapping("/test")
@Api(tags = "测试控制器")
public class TestController {

    @ApiOperation("规则测试")
    @PostMapping("/gzTest")
    public ApiResult<Gzjyjg> gzTest() throws IOException {
        File dataFile = new File("testData/data.json");
        File gzFile = new File("testData/gz.json");
        // 读取数据
        String dataStr = FileUtils.readFileToString(dataFile, StandardCharsets.UTF_8);
        String gzStr = FileUtils.readFileToString(gzFile, StandardCharsets.UTF_8);

        RuleInfo ruleInfo = JSON.parseObject(gzStr, RuleInfo.class);

        List<FormDataObject> formDataObjectList = JSON.parseArray(dataStr).toJavaList(FormDataObject.class);

        List<RuleInfo> ruleInfoList = new ArrayList<>();
        ruleInfoList.add(ruleInfo);

        Gzjyjg gzJyJg = RuleEngine.excute(formDataObjectList, ruleInfoList);

        return ApiResult.success(gzJyJg);
    }
}
